package simplelab;

import java.awt.Point;
import java.util.ArrayList;

/*
 * Class Agent
 * A belief containing all sort of infos about an agent.
 */
public class Agent {
    // Cross-relavant stuff

    public int id;
    public int team;
    public int energy;
    public Point position;
    public int direction;
    // Friend-relavant stuff
    public boolean inWater;
    public Point lastPosition; // Used for water prediction
    public int waited;  // The time this agent waited since last action
    public int shouldWait; // The time this agent should wait before certain action
    public int turnsMade; // The turns the agent made in one place so far
    public ArrayList<Task> tasks;
    public int timeSpentHere;
    public boolean isBlocked;
    public boolean goalBlocked;

    // Agent constructor
    public Agent(int id, int team, int energy, Point position, int direction) {
        this.id = id;
        this.team = team;
        this.energy = energy;
        this.position = position;
        this.direction = direction;
        
        this.inWater = false;
        this.lastPosition = (Point) position.clone();
        this.waited = 0;
        this.shouldWait = 0;
        this.turnsMade = 0;
        this.tasks = new ArrayList<Task>();
        this.timeSpentHere = 0;
        this.isBlocked = false;
        this.goalBlocked = false;
    }

    // Dummy Agent constuctor
    public Agent() {
    }

    public void RefreshIntel(Agent agent) {
        this.energy = agent.energy;
        this.position = agent.position;
        this.direction = agent.direction;

        this.inWater = agent.inWater;
    }

    public boolean IsFacing(Point pos) {
        int A = position.x + position.y, B = position.y - position.x,
                C = pos.x + pos.y, D = pos.y - pos.x;

        if (direction == 0) {
            return C <= A && D <= B;
        } else if (direction == 1) {
            return C >= A && D <= B;
        } else if (direction == 2) {
            return C >= A && D >= B;
        } else {
            return C <= A && D >= B;
        }
    }
/*
    public Point AttackVector() {
        if (direction == 0) {
            return new Point(0, -1);
        } else if (direction == 1) {
            return new Point(1, 0);
        } else if (direction == 2) {
            return new Point(0, 1);
        }
        return new Point(-1, 0);
    }

    public void Face(Point pos) {
        int A = position.x + position.y, B = position.y - position.x,
                C = pos.x + pos.y, D = pos.y - pos.x;

        if (C <= A && D <= B) {
            direction = 0;
        } else if (C >= A && D <= B) {
            direction = 1;
        } else if (C >= A && D >= B) {
            direction = 2;
        } else {
            direction = 3;
        }
    }

    public boolean CanSee(Point pos) {
        if (Math.abs(position.x - pos.x) > 10 || Math.abs(position.y - pos.y) > 10) {
            return false;
        }
        return IsFacing(pos);
    }

    // True if has energy and is not blocked
    public boolean IsUseful() {
        return energy > World.lowEnergy && isBlocked == false;
    }

    // The highest priority task of this agent
    public Task GetTask() {
        int maxPrio = -1;
        Task maxTask = null;
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i).priority > maxPrio || (tasks.get(i).priority == maxPrio
                    && maxTask.Distance() > tasks.get(i).Distance())) {
                maxPrio = tasks.get(i).priority;
                maxTask = tasks.get(i);
            }
        }
        return maxTask;
    */

    Agent setPos(Point pos) {
        this.position = pos;
        return this;
    }
}
